package org.nicocube.airain.domain.server.storable;

import java.util.Date;

import javax.jdo.annotations.Embedded;
import javax.jdo.annotations.EmbeddedOnly;
import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.IdentityType;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;

import org.nicocube.airain.domain.client.character.Gender;

import com.google.appengine.api.users.User;


@PersistenceCapable(identityType = IdentityType.APPLICATION)
public class Player {

	@PrimaryKey
	@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
	private Long order;
	
	@Persistent
	private User user;
	
	@PersistenceCapable
	@EmbeddedOnly
	static class Email {
		@Persistent
		private String email;

		public Email() {
			this.email = null;
		}
		
		public Email(String email) {
			this.email = check(email);
		}
		
		private String check(String email) {
			String trim = email.trim();
			if (trim.matches("^[^@]*@[^@]*$")) return trim;
			else throw new IllegalStateException("trying to set a malformed email");
		}

		public String getEmail() {
			return email;
		}
		
		public void setEmail(String email) {
			this.email = check(email);
		}
	}
	
	@Persistent
	@Embedded
	private Email email;

	@Persistent
	private String firstName;

	@Persistent
	private String lastName;

	@Persistent
	private Date dateOfBirth;

	@Persistent
	private Gender gender;
	@Persistent
	private String country;
	@Persistent
	private String language;
	@Persistent
	private boolean acceptNewsLetter;
	@Persistent
	private boolean acceptPromotion;
	@Persistent
	private boolean acceptPartner;

	
	public Player() {
		email = new Email();
	}

	public Player(User user) {
		this();
		this.user = user;
	}

	public Long getOrder() {
		return order;
	}

	protected void setUser(User user) {
		this.user = user;
	}
	
	public User getUser() {
		return user;
	}

	public String getEmail() {
		return email.getEmail();
	}
	
	public void setEmail(String email) {
		this.email.setEmail(email);
	}

	public String getFirstName() {
		return firstName;
	}

	public void setFirstName(String firstName) {
		this.firstName = firstName;
	}

	public String getLastName() {
		return lastName;
	}

	public void setLastName(String lastName) {
		this.lastName = lastName;
	}

	public Date getDateOfBirth() {
		return dateOfBirth;
	}

	public void setDateOfBirth(Date dateOfBirth) {
		this.dateOfBirth = dateOfBirth;
	}

	public Gender getGender() {
		return gender;
	}

	public void setGender(Gender gender) {
		this.gender = gender;
	}

	public String getCountry() {
		return country;
	}

	public void setCountry(String country) {
		this.country = country;
	}

	public String getLanguage() {
		return language;
	}

	public void setLanguage(String language) {
		this.language = language;
	}

	public boolean isAcceptNewsLetter() {
		return acceptNewsLetter;
	}

	public void setAcceptNewsLetter(boolean acceptNewsLetter) {
		this.acceptNewsLetter = acceptNewsLetter;
	}

	public boolean isAcceptPromotion() {
		return acceptPromotion;
	}

	public void setAcceptPromotion(boolean acceptPromotion) {
		this.acceptPromotion = acceptPromotion;
	}

	public boolean isAcceptPartner() {
		return acceptPartner;
	}

	public void setAcceptPartner(boolean acceptPartner) {
		this.acceptPartner = acceptPartner;
	}
	
}
